package com.nbst.db.datasource;

import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import com.github.pagehelper.PageInterceptor;
import com.mysql.cj.jdbc.MysqlXADataSource;
import com.nbst.db.config.MesDBConfig;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

/**
 * 
 * @ClassName: MesDataSourceConfig
 * @Description:MES数据源配置
 * @author King
 * @date 2020年6月4日
 *
 */
@Configuration
@MapperScan(basePackages = "com.nbst.dao.mapper.mes", sqlSessionTemplateRef = "mesSqlSessionTemplate")
public class MesDataSourceConfig {
	// 配置数据源
	@Bean(name = "mesDataSource")
	public DataSource mesDataSource(MesDBConfig mesConfig) throws SQLException {
		MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();
		mysqlXaDataSource.setUrl(mesConfig.getUrl());
		mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
		mysqlXaDataSource.setPassword(mesConfig.getPassword());
		mysqlXaDataSource.setUser(mesConfig.getUsername());
		mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);

		// 创建Atomikos全局事务，所有的事务注册进来
		AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
		xaDataSource.setXaDataSource(mysqlXaDataSource);
		xaDataSource.setUniqueResourceName("mesDataSource"); 

		xaDataSource.setMinPoolSize(mesConfig.getMinPoolSize());
		xaDataSource.setMaxPoolSize(mesConfig.getMaxPoolSize());
		xaDataSource.setMaxLifetime(58000);
		xaDataSource.setBorrowConnectionTimeout(mesConfig.getBorrowConnectionTimeout());
		xaDataSource.setLoginTimeout(mesConfig.getLoginTimeout());
		xaDataSource.setMaintenanceInterval(mesConfig.getMaintenanceInterval());
		xaDataSource.setMaxIdleTime(mesConfig.getMaxIdleTime());
		xaDataSource.setTestQuery(mesConfig.getTestQuery());
		return xaDataSource;
	}

	@Bean(name = "mesSqlSessionFactory")
	public SqlSessionFactory mesSqlSessionFactory(@Qualifier("mesDataSource") DataSource dataSource) throws Exception {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		// 分页插件
		Interceptor interceptor = new PageInterceptor();
		Properties properties = new Properties();
		// 数据库
		properties.setProperty("helperDialect", "mysql");
		// 是否将参数offset作为PageNum使用
		properties.setProperty("offsetAsPageNum", "true");
		// 是否进行count查询
		properties.setProperty("rowBoundsWithCount", "true");
		// 是否分页合理化
		properties.setProperty("reasonable", "false");
		interceptor.setProperties(properties);
		bean.setPlugins(new Interceptor[] { interceptor });
		bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mes/*.xml"));
		return bean.getObject();
	}

	@Bean(name = "mesSqlSessionTemplate")
	public SqlSessionTemplate mesSqlSessionTemplate(
			@Qualifier("mesSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
		return new SqlSessionTemplate(sqlSessionFactory);
	}
}
